package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.service.TeacherService;

@Controller
@RequestMapping(value = "/teacher")
public class TeacherLeaveController extends BaseController{

	@Autowired
	private TeacherService teacherService;
	private static Logger logger = LoggerFactory.getLogger(TeacherLeaveController.class);	

	/**
	 * 获取请假类别
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getLeaveType", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getLeaveType(HttpServletRequest request)
			throws UnsupportedEncodingException {
		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		try {
			response = teacherService.getLeaveType(response);
		} catch (Exception e) {
			logger.info("getLeaveType:"+ e.toString());
			response.setReturnMsg(1,"获取异常！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 教练请假
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addTeacherLeave", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addTeacherLeave(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 教练ID
			String teacherId = request.getParameter("teacherId");
			// 请假开始时间
			String leaveStartTime = request.getParameter("leaveStartTime");
			// 请假结束时间
			String leaveEndTime = request.getParameter("leaveEndTime");
			//请假类型
			String leaveType = request.getParameter("leaveType");
			//请假原因
			String leaveCause = request.getParameter("leaveCause");
			if(teacherId != null && !teacherId.isEmpty()
					&& leaveStartTime != null && !leaveStartTime.isEmpty()
					&& leaveEndTime != null && !leaveEndTime.isEmpty()){ 
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("teacherId",teacherId);
				paramsMap.put("leaveStartTime",leaveStartTime);
				paramsMap.put("leaveEndTime",leaveEndTime);
				paramsMap.put("leaveType",leaveType);
				paramsMap.put("leaveCause",leaveCause);
				//获取课程信息
				response = teacherService.addTeacherLeave(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}
		} catch (Exception e) {
			logger.info("addTeacherLeave:"+ e.toString());
			response.setReturnMsg(1,"请假异常！");
		} finally {
			return response.toJSON();
		}
	}
}
